﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AjaxControlFramework;

using BingMapsSample.Components;

namespace BingMapsSample
{
    public partial class ucSearchResultsList : BaseAjaxControl
    {
        //------// Properties \\--------------------------------------------\\
        private int _listItemCount = 0;
        public int ListItemCount
        {
            get { return ++_listItemCount; }
        }


        private List<Place> _boundPlaces = null;
        public List<Place> BoundPlaces
        {
            get
            {
                if (_boundPlaces == null)
                {
                    _boundPlaces = new List<Place>();
                }

                return _boundPlaces;
            }
            set { _boundPlaces = value; }
        }


        public Place CurrentPlace
        {
            get { return BoundPlaces[_listItemCount - 1]; }
        }
        //------\\ Properties //--------------------------------------------//



        //------// Constructors \\------------------------------------------\\
        public ucSearchResultsList()
            : base("~/ucSearchResultsList.ascx")
        { }
        //------\\ Constructors //------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                Location location = new BingMapsService().GeocodeIPAddress("173.203.23.242");

                if (location != null && location.Address != null)
                {
                    literalIPAddress.Text = "173.203.23.242 - " + location.Address.Address1 + " " + location.Address.Address2 + ", " + location.Address.City + ", " + location.Address.State + " " + location.Address.ZipCode;
                }


                if (!ParentPage.IsPostBack && ParentPage.SearchIsActive)
                {
                    SearchResultsController.ClearSearchResults();
                    BindResultsListView();
                }
            }
            catch (System.Exception exception)
            {
                // You should probably catch and handle this...meh...
                throw exception;
            }
        }


        public void BindResultsListView()
        {
            resultsListView.DataSource = SearchResultsController.GetSearchResults(ParentPage.Keyword);
            resultsListView.DataBind();

            UpdateAjaxState();
        }


        public void UpdateAjaxState()
        {
            if (resultsListView.DataSource == null)
            {
                base.AjaxContext.State["Places"] = null;
            }
            else
            {
                List<Place> dataSource = resultsListView.DataSource as List<Place>;

                if (dataSource.Count == 0) 
                { 
                    base.AjaxContext.State["Places"] = null;
                    return;
                }

                int difference = dataSource.Count - resultsDataPager.StartRowIndex;
                if (difference < resultsDataPager.PageSize) { resultsDataPager.PageSize = difference; }

                base.AjaxContext.State["Places"] = dataSource.GetRange(resultsDataPager.StartRowIndex, resultsDataPager.PageSize);
            }
        }



        #region "Event Handlers"

        protected void resultsListView_ItemDataBound(object sender, ListViewItemEventArgs args)
        {
            if (args.Item.ItemType == ListViewItemType.DataItem)
            {
                ListViewDataItem dataItem = (ListViewDataItem)args.Item;
                BoundPlaces.Insert(dataItem.DisplayIndex, dataItem.DataItem as Place);
            }
        }


        [AjaxControlMethod]
        public void ShowPreviousPage(int currentPage)
        {
            resultsDataPager.SetPageProperties(((currentPage - 2) * resultsDataPager.PageSize), resultsDataPager.PageSize, false);
            BindResultsListView();
        }


        [AjaxControlMethod]
        public void ShowNextPage(int currentPage)
        {
            resultsDataPager.SetPageProperties((currentPage * resultsDataPager.PageSize), resultsDataPager.PageSize, false);
            BindResultsListView();
        }


        [AjaxControlMethod]
        public void ShowPage(int currentPage)
        {
            resultsDataPager.SetPageProperties(((currentPage - 1) * resultsDataPager.PageSize), resultsDataPager.PageSize, false);
            BindResultsListView();
        }


        [AjaxControlMethod]
        public void ShowFirstPage()
        {
            resultsDataPager.SetPageProperties(0, resultsDataPager.PageSize, false);
            BindResultsListView();
        }


        [AjaxControlMethod]
        public void ShowLastPage()
        {
            List<Place> searchResults = SearchResultsController.GetSearchResults(ParentPage.Keyword);

            int placeCount = searchResults.Count;
            int startRowIndex = (((int)(placeCount / resultsDataPager.PageSize)) * (resultsDataPager.PageSize - 1));

            resultsDataPager.SetPageProperties(startRowIndex, resultsDataPager.PageSize, false);

            resultsListView.DataSource = searchResults;
            resultsListView.DataBind();

            UpdateAjaxState();
        }

        #endregion
        //------\\ Methods //-----------------------------------------------//
    }
}